package my.teco.plee.popwindowdemo;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

/**
 * 在这篇文章 http://www.jianshu.com/p/6db4fae05257 的demo基础上做修改.
 */
public class PopMainActivity extends AppCompatActivity implements View.OnClickListener {

    private PopupWindow mPopupWindow;
    private Button mPopupFromLeftBtn;
    private Button mPopupFromBottomBtn;
    private int mPopupWindowWidth;
    private int mPopupWindowHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_main);
        mPopupFromLeftBtn = (Button) findViewById(R.id.btn1);
        mPopupFromBottomBtn = (Button) findViewById(R.id.btn2);
        mPopupFromLeftBtn.setOnClickListener(this);
        mPopupFromBottomBtn.setOnClickListener(this);
        initPopupWindow();
    }

    private void initPopupWindow() {
        initLeftPopupWindow();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                //设置动画效果
                mPopupWindow.setAnimationStyle(R.style.AnimationHorizontal);
                mPopupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
                boolean outsideTouchable = mPopupWindow.isOutsideTouchable();
                Toast.makeText(PopMainActivity.this, "outsideTouchable = " + outsideTouchable,
                        Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn2:
                mPopupWindow.setAnimationStyle(R.style.AnimationVertical);
                // showAsDropDown(View anchor, int xoff, int yoff, int gravity) 从 API-19 开始引入.
                mPopupWindow.showAsDropDown(mPopupFromBottomBtn, mPopupWindowWidth, 0);
                break;
        }
    }

    /**
     * 创建PopupWindow
     */
    protected void initLeftPopupWindow() {
        // TODO: 15/10/9
        //获取自定义布局文件activity_pop_left.xml 布局文件
        final View popupWindowView = getLayoutInflater().inflate(R.layout.activity_pop_left, null, false);

        // popupWindow 的宽高
        mPopupWindowWidth = 250;
        mPopupWindowHeight = ViewGroup.LayoutParams.MATCH_PARENT;

        //创建Popupwindow 实例，200，LayoutParams.MATCH_PARENT 分别是宽高
        mPopupWindow = new PopupWindow(popupWindowView, mPopupWindowWidth, mPopupWindowHeight, true);

        //点击其他地方消失
        popupWindowView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupWindowView != null && popupWindowView.isShown()) {
                    mPopupWindow.dismiss();
                    mPopupWindow = null;
                }
                return false;
            }
        });

        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
        Button button1 = (Button) popupWindowView.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "当前点击的是我", Toast.LENGTH_SHORT).show();
                System.out.println("我被点击了");
            }
        });
    }
}